package com.example.demo6.ui.lose;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.demo6.R;
import com.example.demo6.SQLite.LostGoods;
import com.example.demo6.databinding.FragmentLoseBinding;

import java.util.ArrayList;
import java.util.List;

public class loseFragment extends Fragment {
    RecyclerView recyclerView;
    LoseViewModel loseViewModel;

    private List<LostGoods> lostGoodsList;

    LostAdapter lostAdapter;

    private FragmentLoseBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding=FragmentLoseBinding.inflate(inflater, container, false);
        View root=binding.getRoot();
        recyclerView=root.findViewById(R.id.lostrecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        lostGoodsList=new ArrayList<>();
        lostAdapter=new LostAdapter(getContext(),lostGoodsList);
        recyclerView.setAdapter(lostAdapter);
        LoseViewModel recyclerViewViewModel = new ViewModelProvider(this).get(LoseViewModel.class);
        recyclerViewViewModel.getlistLiveData().observe(getViewLifecycleOwner(), new Observer<List<LostGoods>>() {
            @Override
            public void onChanged(List<LostGoods> lostGoods) {
                lostGoodsList.clear();
                lostGoodsList.addAll(lostGoods);
                lostAdapter.notifyDataSetChanged();
            }
        });

        return root;


    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState);
//
//        Button buttonGoToLogin = view.findViewById(R.id.lostbtn);
//        buttonGoToLogin.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                NavController navController = Navigation.findNavController(v); // 使用按钮视图找到NavController
//                navController.navigate(R.id.edit_lose_informationFragment); // 根据action_id导航
//            }
//        });
    }
}